package com.example.blog.controller;

import com.example.blog.model.Article;
import com.example.blog.model.User;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;


@Controller
@RequestMapping("article")
public class ArticleController extends BaseController {

    private SqlSession sqlSession;

    public ArticleController(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }
    @RequestMapping(value = "articleIn" ,method = RequestMethod.GET)
    private  String articleIn(){
        return null;

    }
    @RequestMapping(value = "create", method = RequestMethod.POST)
    private String create(Article article) {
        sqlSession.insert("article.create", article);
        User user = sqlSession.selectOne("user.queryUserArticlesById", article.getUserId());
        session.setAttribute("user", user);
        return "redirect:/user/index.jsp";
    }


    @RequestMapping(value = "queryArticleById/{id}", method = RequestMethod.GET)
    private String queryArticleById(@PathVariable("id") int id) {
        Article article = sqlSession.selectOne("article.queryUserArticleById", id);
        session.setAttribute("article", article);
        return "redirect:/article.jsp";
    }


}
